﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ImageProcessing.DataEntryDialogs;
using ImageProcessing.IntermediateClasses;
using ImageProcessing.Utilities;

namespace ImageProcessing
{
    public partial class FourierTransform : Form
    {
        Bitmap originalImage;
        FourierOperations fourierOps;

        public FourierTransform(Image image)
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
            this.pictureBoxOriginal.Image = image;
            originalImage = new Bitmap(image);
            fourierOps = new FourierOperations(this.originalImage);
        }

        private void btnFourier_Click(object sender, EventArgs e)
        {
            try
            {
                fourierOps.Transform();
                if (this.rdRed.Checked)
                {
                    this.pictureBoxFourier.Image = fourierOps.RedMagnitude();
                }
                else
                    if (this.rdGreen.Checked)
                    {
                        this.pictureBoxFourier.Image = fourierOps.GreenMagnitude();
                    }
                    else
                        if (this.rdBlue.Checked)
                        {
                            this.pictureBoxFourier.Image = fourierOps.BlueMagnitude();
                        }
            }
            catch
            {
                MessageBox.Show("Error during fourier frequency transform operation and magnitude display.");
            }
        }

        private void btnReverseFourier_Click(object sender, EventArgs e)
        {
            try
            {
                this.pictureBoxFourier.Image = fourierOps.ReverseFourier();
            }
            catch
            {
                MessageBox.Show("Error converting from frequency to spatial domain.");
            }
        }

        private void butterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Dimentions dim = new Dimentions();
                FrequencyFilterDialog dialog = new FrequencyFilterDialog(ref dim, true);
                dialog.ShowDialog();
                double distance = dim.DoubleValue;
                int butterworthN = dim.Value;
                this.fourierOps.ButterworthLowPass(distance, butterworthN);
                this.pictureBoxFourier.Image = this.fourierOps.ReverseFourier();
            }
            catch
            {
                MessageBox.Show("Error applying Butterworth low pass filter");
            }
        }

        private void idealToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Dimentions dim = new Dimentions();
                FrequencyFilterDialog dialog = new FrequencyFilterDialog(ref dim, false);
                dialog.ShowDialog();
                double distance = dim.DoubleValue;
                //double distance = double.Parse(this.txtDistance.Text.ToString());
                this.fourierOps.IdealLowPass(distance);
                this.pictureBoxOriginal.Image = this.fourierOps.ReverseFourier();
            }
            catch
            {
                MessageBox.Show("Error applying ideal low pass filter");
            }

        }

        private void idealToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Dimentions dim = new Dimentions();
            FrequencyFilterDialog dialog = new FrequencyFilterDialog(ref dim, false);
            dialog.ShowDialog();
            double distance = dim.DoubleValue;
            //double distance = double.Parse(this.txtDistance.Text.ToString());
            this.fourierOps.IdealHighPass(distance);
            this.pictureBoxOriginal.Image = this.fourierOps.ReverseFourier();
        }

        private void gaussianToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Dimentions dim = new Dimentions();
            FrequencyFilterDialog dialog = new FrequencyFilterDialog(ref dim, false);
            dialog.ShowDialog();
            double distance = dim.DoubleValue;
            //double distance = double.Parse(this.txtDistance.Text.ToString());
            this.fourierOps.GaussianLowPass(distance);
            this.pictureBoxOriginal.Image = this.fourierOps.ReverseFourier();
        }

        private void gaussianToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Dimentions dim = new Dimentions();
            FrequencyFilterDialog dialog = new FrequencyFilterDialog(ref dim, false);
            dialog.ShowDialog();
            double distance = dim.DoubleValue;
            //double distance = double.Parse(this.txtDistance.Text.ToString());
            this.fourierOps.GaussianHighPass(distance);
            this.pictureBoxOriginal.Image = this.fourierOps.ReverseFourier();
        }

        private void butterworthToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Dimentions dim = new Dimentions();
                FrequencyFilterDialog dialog = new FrequencyFilterDialog(ref dim, true);
                dialog.ShowDialog();
                double distance = dim.DoubleValue;
                int butterworthN = dim.Value;
                this.fourierOps.ButterworthHighPass(distance, butterworthN);
                this.pictureBoxFourier.Image = this.fourierOps.ReverseFourier();
            }
            catch
            {
                MessageBox.Show("Error applying Butterworth low pass filter");
            }
        }
    }
}
